
var InvestorModule = function () {
	
	var handleResourceTree = function() {
		$('#empGroupTree').jstree({
			'plugins' : [ "wholerow", "checkbox", "types" ],
			'core' : {
				"themes" : {
					"responsive" : false
				},
				'data' : {
					"url" : ctx + "/investor/empTree",
					"data" : function(node) {

					}
				}
			},
			"types" : {
				"default" : {
					"icon" : "fa fa-folder icon-warning icon-lg"
				},
				"file" : {
					"icon" : "fa fa-file icon-warning icon-lg"
				}
			}
		}).on("changed.jstree", function(e, data) {
			$('#urls').val(data.selected);
		});
	};
    var editable = function () {
        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                    "lengthMenu": [
                        [10, 15, 20, 50],
                        [10, 15, 20, 50]
                    ],
                    "autoWidth":false,
                    "displayLength": 10,
                    "scrollX": true,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "记录数为0",
                    },
                    "columnDefs": [
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [0]
						},
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [1]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [2]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [3]
						}, 
						{
						    "data": null,
						    'sortable': false,
						    "defaultContent": "<center>-</center>",
						    "targets": [4]
						}
						
                    ],
                    "columns": [
                       { "data": "message" },
                       { "data": "source" },
                       { "data": "skipType" },
                       { "data": "createTime" }
                   ],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "ajaxSource": ctx + "/message/query",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   nRow.children[0].type = aData.type;
                	   nRow.children[0].message = aData.message;
                	   nRow.children[0].createTime = aData.createTime;
                	   var type = aData.type;
                	   var message = aData.message;
                	   var small = message.substr(0, 20);
                	   if(message.length >20){
                		   $('td:eq(0)', nRow).html("<p title="+clearBr(message)+">"+small+'......</p>');
                	   }
                	   if(type == "2"){
                		   $('td:eq(2)', nRow).html('<center>Pad端<center>');
                	   }else if(type == "1"){
                		   $('td:eq(2)', nRow).html('<center>PC端<center>');
                	   }else if(type == "3"){
                		   $('td:eq(2)', nRow).html('<center>投后APP<center>');
                	   }else{
                		   $('td:eq(2)', nRow).html('<center>-<center>');
                	   }
                	   $('td:eq(4)', nRow).html('<center><a class="detail fa fa-bars fa-lg " title="详情" href="javascript:;"></a><center>');
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = "";
	                   var s;
	                   for (var i = 0; i < dataSet.length; i++) {
	                       s = dataSet[i];
	                   	   if (s.name == 'sEcho') {
	                   		   json = json + '"echo":' + s.value;
		                   } else if (s.name == 'iDisplayStart') {
		                	   json = json + ',"page":' + s.value;
		                   } else if (s.name == 'iDisplayLength') {
		                	   json = json + ',"pageSize":' + s.value;
		                   }
	                   }
            	       var form = $("form#queryForm").serializeJSON();
                   	   var js = form.replace('}', ',');
                   	   json = js + json + "}";
                       $.ajax({
                       	   "contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ctx + "/message/query",
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
                // 通知详情
                $('#modelEditable').on("click", "a.detail", (function (e) {
                	//$('#btn_detail').click();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    var type = nRow.children[0].type;
                    var message = nRow.children[0].message;
                    var createTime = nRow.children[0].createTime;
                    if(type == 1){
                    	document.getElementById("send_object").innerHTML="PC 端";
                    }else if(type == 2){
                    	document.getElementById("send_object").innerHTML="PC 端";
                    }
                    $('#message_detail').val(message);
                    document.getElementById("create_time").innerHTML = createTime
                    $('#myDetail').modal('show');
                    
                }));
                $('#btn_query').click(function(){
                	oTable.fnClearTable();
                });
            }

        };
    }();
    return {
        init: function () {
        	editable.init();
        	handleResourceTree();
        }
    };
}();

var error = $('.alert-danger');
var modalbody = $('.modal-body');
error.hide();
$('#btn_send_message').click(function(){
	var message = $('#message').val();
	if(message.length >1024 || message.length <=0){
		modalbody.css("height","370px");
		error.show();
		return;
	}
	var json = $("form#sendForm").serializeJSON();
	$('#btn_send_message').attr('disabled',true);
	$('#btn_close_send').click();
	$.ajax({
    	"contentType":"application/json; charset=UTF-8",
        "dataType" : 'json',
        "type" : "POST",
        "url" : ctx + "/message/doAdd",
        "data" : json,
        "success" : function(msg){
        	$('#btn_close_send').click();
        	$('#btn_send_message').attr('disabled',false);
        	$.teninedialog({
                title:'系统提示',
                content:'发送通知成功',
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	location.reload();
                }
            });
        },
        "error":function(msg){
        	$.teninedialog({
                title:'系统提示',
                content:'发送通知失败了',
                showCloseButton:false,
                otherButtons:["确定"],
                otherButtonStyles:['btn-primary'],
                bootstrapModalOption:{keyboard: true},
                clickButton:function(sender,modal,index){
                	location.reload();
                }
            });
        }
    });
});

$('#btn_send').click(function(){
	$('#message').val('');
	modalbody.css("height","300px");
	error.hide();
});

//去除换行、空格
function clearBr(key)
{
	key = key.replace(/\s+/g, "");	
    key = key.replace(/<\/?.+?>/g,"");
    key = key.replace(/[\r\n]/g, "");
    return key;
}

